<?php
namespace Popshouse\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Security\Core\SecurityContext;
use Popshouse\SiteBundle\Entity\News;
use Popshouse\AdminBundle\Form\NewsForm;

use Popshouse\AdminBundle\Helper\Arr;
use Popshouse\AdminBundle\Helper\Image;


class NewsController extends Controller
{
    /**
     * @extra:Template()
     */
    public function indexAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        
        $query = $em->createQuery("SELECT n FROM Popshouse\SiteBundle\Entity\News n ORDER BY n.sort ASC");
        $news = $query->getResult();
        return array('news' => $news, 'active'=>'news');
    }
    
  
    public function sortAction()
    {
        $em = $this->get('doctrine.orm.entity_manager');
        
        $query = $em->createQuery("SELECT n FROM Popshouse\SiteBundle\Entity\News n ORDER BY n.sort ASC");
        $news = $query->getResult();
        
        $order = $this->get('request')->request->get('order');
    
        if(!empty($order)){
            $orders = Arr::decodeJSArray($order);
            foreach($news as $item){
                $item->setSort(array_search($item->getId(), $orders));
            }
            $em->flush();
            return new Response(json_encode(array('message'=>'Volgorde Opgeslaan')));
        }
        
        return $this->render('PopshouseAdminBundle:News:sort.html.twig', array('news' => $news, 'active'=>'news'));
    }
    
    public function deleteAction($id = 0){
		$em = $this->get('doctrine.orm.entity_manager');
        $news = $em->find('Popshouse\SiteBundle\Entity\News', $id);
        if($news===null || !$news->getId())
           throw new \Exception("Dit bericht werd niet gevonden");
         	
        
        $em->remove($news);
        $em->flush();
        
         return new Response(json_encode(array('message'=>'Verwijdert')));
        
    }

    /**
     * @extra:Template()
     */
    public function formAction($id = 0){
        
    	$news = new News();
    	
        if($id != 0){
            $em = $this->get('doctrine.orm.entity_manager');
             //$news = new News();
            $news = $em->find('Popshouse\SiteBundle\Entity\News', $id);
            
            if($news===null || !$news->getId())
            	throw new \Exception("Dit bericht werd niet gevonden");
            	
            $news->loadTranslation();
        }
        
       	$form = $this->get('form.factory')->createNamed(new NewsForm(),'news',$news);
        $request = $this->get('request');
	    if ($request->getMethod() == 'POST') {
	        $form->bindRequest($request);
	        
	        if ($form->isValid()) {
	
	            $em = $this->get('doctrine.orm.entity_manager');
	            if(!$id)
	            	$em->persist($news);
	            $em->flush();
	            
	            $afbeelding = $form->get('afbeelding')->getData();
	            if(!empty($afbeelding)){
		            $image = new Image($form->get('afbeelding')->getData());
		            $image->save($news->getImagePath());
		            $image->resize(50,50);
		            $image->save($news->getImagePath(true));
	            }
	            
	            $this->get('session')->setFlash('notice', 'Nieuws bewaard!');
	            return new RedirectResponse($this->generateUrl('admin_news'));
	        }
	    }

        return array('form' => $form->createView(), 'news' => $news, 'active'=>'news');
    }
    
 
}
